const formatLineData = (floorplanRoomList) => {
	const coords = floorplanRoomList.map(roomData => {
		const cornerList = roomData.cornerList.map(corner => {
			return [corner.x, corner.y];
		});

		return {
			name: roomData.name,
			coords: [...cornerList, cornerList[0]]
		}
	});

	return {
		type: 'lines',
		coordinateSystem: 'cartesian2d',
		polyline: true,
		data: coords,
		lineStyle: {
			opacity: 1,
			color: '#000000', // 黑色
			width: 3,
			type: 'solid'
		},
		opacity: 1,
		label: {
			show: true,
			fontSize: 14,
			color: '#333333',
			backgroundColor: 'rgba(255, 255, 255, 1)',
			padding: [6, 10],
			borderRadius: 6,
			borderColor: '#000000',
			borderWidth: 1
		}
	};
};

const formatPointData = (floorplanRoomList) => {
	const setList = new Set();

	floorplanRoomList.forEach(roomData => {
		roomData.cornerList.forEach(corner => {
			setList.add(corner);
		});
	});

	return {
		type: 'scatter',
		coordinateSystem: 'cartesian2d',
		data: [...setList].map(({ x, y }) => ({
			name: '', // 暂时用空字符串代替
			value: [x, y]
		})),
		symbolSize: 18,
		color: '#4CAF50', // 主绿色
		opacity: 1,
		itemStyle: {
			borderColor: '#FF6B35',
			borderWidth: 2
		},
		label: {
			show: true,
			position: 'top',
			color: '#333333',
			opacity: 1,
			fontSize: 12,
			backgroundColor: 'rgba(255, 255, 255, 1)',
			padding: [4, 8],
			borderRadius: 4,
			borderColor: '#4CAF50',
			borderWidth: 1,
			formatter: function (params) {
				const [x, y] = params.value;
				return `(${x}, ${y})`;
			}
		}
	}
};

const fromatRoomLableList = (floorplanRoomList) => {
	const roomLabelList = [];

	floorplanRoomList.forEach(roomData => {
		const { name, label } = roomData;

		roomLabelList.push({ name, label });
	});

	const dataList = roomLabelList.map(roomLabel => {
		return {
			name: roomLabel.name,
			value: roomLabel.label
		}
	});

	return {
		type: 'scatter',
		coordinateSystem: 'cartesian2d',
		data: dataList.map(({ name, value }) => {
			return {
				name: name,
				value: [value.x, value.y]
			};
		}),
		symbol: 'rect',
		symbolSize: 20,
		color: '#FF8C42', // 浅橙色
		opacity: 1,
		itemStyle: {
			borderColor: '#FF6B35',
			borderWidth: 2
		},
		label: {
			show: true,
			position: 'top',
			color: '#333333',
			opacity: 1,
			fontSize: 14,
			fontWeight: 'bold',
			backgroundColor: 'rgba(255, 255, 255, 1)',
			padding: [4, 8],
			borderRadius: 4,
			borderColor: '#FF8C42',
			borderWidth: 1,
			formatter: function (params) {
				return '(' + params.name + ')';
			}
		}
	}
};

const formatData = (floorplanRoomList) => {
	const lineData = formatLineData(floorplanRoomList);
	const pointData = formatPointData(floorplanRoomList);
	const roomLabelData = fromatRoomLableList(floorplanRoomList);

	return {
		lineData,
		pointData,
		roomLabelData
	}
}

export {
	formatData
}